import json
import pandas as pd

path = [
    "./data/api_nodes_estimator.csv",
    "./data/m-a_edges.csv",
    "./data/mashup_nodes_estimator.csv",
    "./data/split_nodes.csv",
    "./data/tranfer_nodes.csv",
]
accessibility_path = [
    "./data/raw/accessibility/api_accessibility/api_version_accessbiliby-1.json",
    "./data/raw/accessibility/api_accessibility/api_version_accessbiliby-2.json",
    "./data/raw/accessibility/api_accessibility/api_version_accessbiliby-3.json",
    "./data/raw/accessibility/api_accessibility/api_version_accessbiliby-4.json",
    "./data/raw/accessibility/api_accessibility/api_version_accessbiliby-5.json",
]


def Task1():
    """
    每个Mashup包含的Web API个数
    """
    df = pd.read_csv(path[1], sep="\t")
    dfr = df.groupby("source", as_index=False).count()
    dfr.to_csv("./output/1-1Mashup包含的API个数.csv", index=0)


def Task2():
    """
    每个Web API被使用的次数
    """
    df = pd.read_csv(path[1], sep="\t")
    dfr = df.groupby("target", as_index=False).count()
    dfr.to_csv("./output/1-2API被使用的次数.csv", index=0)


def Task3():
    """
    Web API提供商(URL网址)发布Web API的个数
    """
    wap_dict = {}
    for i in range(5):
        with open(accessibility_path[i], "r") as a:
            res = json.load(a)
            for it in res:
                # WAP (Web Api Provider)
                WAP = it["version_title"].split()[0]
                if WAP in wap_dict.keys():
                    wap_dict[WAP] += 1
                else:
                    wap_dict[WAP] = 1
    pd.DataFrame(list(wap_dict.items()), columns=["API Provider", "APIs"]).to_csv(
        "./output/1-3提供商发布API的个数.csv", index=0
    )

Task1()
Task2()
Task3()
